#include <iostream>
#include <boost/noncopyable.hpp>
using namespace std;

template <class Derived>
class Singleton : public boost::noncopyable
{
public:
    static Derived *instance() {
        if (_singleton == NULL) {
            _singleton = new Derived; 
        }
        return _singleton;
    }
    static void destory() {
        if (_singleton != NULL) {
            delete _singleton; 
            _singleton = NULL;
        }
    }
private:
    static Derived *_singleton = NULL;
};


class Machine : public Singleton<Machine>
{
public:
    void init(int a){_a = a;}
    void print()
    {
        cout << "Print: " << _a << endl; 
    }
private:
    int _a;
};

int main()
{
    cout << "--- Start ---" << endl;
    Machine::instance()->init(13); 
    Machine::instance()->print();

    typedef Singleton<Machine> machine;
    machine::instance()->init(33);
    machine::instance()->print();
    machine::destory();
    cout << "--- End ---" << endl;
    return 0;
}
